package com.czy.owner.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.MyPackageEntity;
import com.czy.owner.ui.packagecardproject.MyPackageCardDetailActivity;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.widget.SlidingButtonView;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wangyang on 2017/4/19 0019.
 * unlimitedCount 是否有次数限制 hep 2018/01/31
 */

public class MyPackageCardProjectAdapter2 extends RecyclerArrayAdapter<MyPackageEntity> implements SlidingButtonView.IonSlidingButtonListener {
    private Context ctx;
    private IonSlidingViewClickListener mIDeleteBtnClickListener;
    private SlidingButtonView mMenu = null;

    public MyPackageCardProjectAdapter2(Context context) {
        super(context);
        this.ctx = context;

    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyPackageCardProjectViewHolder(parent);
    }

    class MyPackageCardProjectViewHolder extends BaseViewHolder<MyPackageEntity> {
        private LinearLayout ll_bg_b;
        private TextView tv_package_name;
        private EasyRecyclerView easy_recycleview_sub;
        private TextView tv_use_time;
        private TextView tv_use_store;
        private ImageView img_expired;
        private MyPackageSubAdapter myPackageSubAdapter;
        private TextView tv_give_amount;

        private LinearLayout tv_delete;
        public ViewGroup layout_content;
        private RelativeLayout relat_give_amount;

        public MyPackageCardProjectViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_new_mypackage);
            ll_bg_b = $(R.id.ll_bg_b);
            tv_package_name = $(R.id.tv_package_name);
            easy_recycleview_sub = $(R.id.easy_recycleview_sub);
            tv_use_time = $(R.id.tv_use_time);
            tv_use_store = $(R.id.tv_use_store);
            img_expired = $(R.id.img_expired);

            relat_give_amount = $(R.id.relat_give_amount);
            tv_give_amount = $(R.id.tv_give_amount);
            layout_content = $(R.id.layout_content);
            tv_delete = $(R.id.tv_delete);
            mIDeleteBtnClickListener = (IonSlidingViewClickListener) ctx;
            ((SlidingButtonView) itemView).setSlidingButtonListener(MyPackageCardProjectAdapter2.this);
        }

        @Override
        public void setData(final MyPackageEntity model) {
            layout_content.getLayoutParams().width = PhoneUtils.getPhoneWidth(ctx);
            layout_content.setPadding(PhoneUtils.dip2px(ctx, 12), 0, PhoneUtils.dip2px(ctx, 12), 0);
            //tv_delete.setPadding(PhoneUtils.dip2px(ctx,-10),0,PhoneUtils.dip2px(ctx,10),0);
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv_delete.getLayoutParams();
            lp.setMargins(PhoneUtils.dip2px(ctx, 2), 0, PhoneUtils.dip2px(ctx, 10), 0);
            tv_delete.setLayoutParams(lp);
            String status = model.getStatus();
//            if (model.getGiveAmount() > 0) {
//                relat_give_amount.setVisibility(View.VISIBLE);
//                tv_give_amount.setText("¥" + DecimalUtil.formatNum(model.getGiveAmount() + ""));
//            } else {
//                relat_give_amount.setVisibility(View.GONE);
//
//            }
            if (status.equals("normal")) {
                tv_delete.setVisibility(View.GONE);
            } else {
                tv_delete.setVisibility(View.VISIBLE);
            }
            layout_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //判断是否有删除菜单打开
                    if (menuIsOpen()) {
                        closeMenu();//关闭菜单
                    } else {
                        int n = getLayoutPosition();
                        mIDeleteBtnClickListener.onItemClick(v, n, model);
                    }
                }
            });


            tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int n = getLayoutPosition();
                    mIDeleteBtnClickListener.onDeleteBtnCilck(v, n, model);
                }
            });


            long startTime = model.getStartTime();
            long endTime = model.getEndTime();

            if (startTime == 0 || endTime == 0) {
                tv_use_time.setText("有效期: 永久有效");
            } else {
                tv_use_time.setText("有效期: " +TimeUtils.formatTimeDifference4(startTime) + "-" + TimeUtils.formatTimeDifference4(endTime));
            }
            tv_use_store.setText("限" + model.getStoreName() + "使用");
            tv_package_name.setText(model.getPackageName());
            if (!status.equals("normal")) {
                img_expired.setVisibility(View.VISIBLE);
                ll_bg_b.setBackgroundResource(R.drawable.package_top_gray);
                tv_use_time.setTextColor(ctx.getResources().getColor(R.color.my_package_used_title));
                tv_use_store.setTextColor(ctx.getResources().getColor(R.color.my_package_used_title));
            } else {
                img_expired.setVisibility(View.GONE);
                ll_bg_b.setBackgroundResource(R.drawable.package_top_blue);
                tv_use_time.setTextColor(ctx.getResources().getColor(R.color.my_package_normal_title));
                tv_use_store.setTextColor(ctx.getResources().getColor(R.color.appThemeColor));
            }

            easy_recycleview_sub.setLayoutManager(new LinearLayoutManager(ctx));
            easy_recycleview_sub.setAdapter(myPackageSubAdapter = new MyPackageSubAdapter(ctx, status));
            List<MyPackageEntity.ItemListBean> mList = new ArrayList<>();
            if (model.getItemList() != null && model.getItemList().size() != 0) {
                for (int i = 0; i < model.getItemList().size(); i++) {
                    if (i < 5) {
                        MyPackageEntity.ItemListBean subPackageModel = model.getItemList().get(i);
                        mList.add(subPackageModel);
                    } else {
                        break;
                    }
                }
            }

            myPackageSubAdapter.addAll(mList);
            myPackageSubAdapter.setOnItemClickListener(new OnItemClickListener<MyPackageEntity.ItemListBean>() {
                @Override
                public void onItemClick(View view, int position, MyPackageEntity.ItemListBean mSubData) {
                    if (PhoneUtils.isNetworkConnected(ctx)) {
                        Intent ti = new Intent(ctx, MyPackageCardDetailActivity.class);
                        ti.putExtra("userPackageId", model.getUserPackageId());
                        ctx.startActivity(ti);
                    }
                }
            });

        }
    }


    class MyPackageSubAdapter extends RecyclerArrayAdapter<MyPackageEntity.ItemListBean> {
        private String status = "normal";

        public MyPackageSubAdapter(Context context, String status) {
            super(context);
            this.status = status;

        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyPackageSubViewHolder(parent);
        }

        class MyPackageSubViewHolder extends BaseViewHolder<MyPackageEntity.ItemListBean> {
            private LinearLayout llSubBg;
            private TextView tv_name;
            private TextView tv_sum_number;
            private TextView tv_used_number;

            public MyPackageSubViewHolder(ViewGroup parent) {
                super(parent, R.layout.item_my_package_sub);
                llSubBg = $(R.id.ll_sub_bg);
                tv_name = $(R.id.tv_name);
                tv_sum_number = $(R.id.tv_sum_number);
                tv_used_number = $(R.id.tv_used_number);


            }

            @Override
            public void setData(final MyPackageEntity.ItemListBean model) {
                tv_name.setText(model.getItemName());
                if(status.equals("normal")){
                    llSubBg.setBackgroundResource(R.drawable.package_sub_item_blue);
                }else {
                    llSubBg.setBackgroundResource(R.drawable.package_sub_item_gray);
                }

                if (!model.isUnlimitedCount()) {
                    tv_sum_number.setText("不限次");
                    tv_used_number.setText("不限次");
                } else {
                    tv_sum_number.setText(model.getTotalCount() + "");
                    tv_used_number.setText(model.getCount() + "");
                }
            }
        }
    }


    /**
     * 删除菜单打开信息接收
     */
    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    /**
     * 滑动或者点击了Item监听
     *
     * @param slidingButtonView
     */
    @Override
    public void onDownOrMove(SlidingButtonView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }


    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    /**
     * 判断是否有菜单打开
     */
    public Boolean menuIsOpen() {
        return mMenu != null;
    }


    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position, MyPackageEntity model);

        void onDeleteBtnCilck(View view, int position, MyPackageEntity model);
    }

    public void removeData(int position) {
        mObjects.remove(position);
        notifyItemRemoved(position);

    }

}
